<?php
  /**
   <## license ##>
<## sydonie ##>
*/



  /**
   *
   * @file  Strings.php utilities to manage strings
   *
   * @author Hervé Le Crosnier, Jean-Marc Lecarpentier
   * @since January 2008
   *
   */



class Outils_Chaines {


  /**
   * randomString returns a random string
   * @param $length length of the string
   * @param $type lowercase / uppercase / numeric
   * @return a string
   *
   */

  public static function randomString($length = 10, $type = "lowercase") {
    $id = "";
    srand();
    switch ($type) {
    case "uppercase" :
      $tokens = array("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9");
      break;

    case "numeric" :
      $tokens = array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9");
      break;

    default :
    case "lowercase" :

      $tokens = array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9");
      break;
    }
    for ($i = 0 ; $i<$length ; $i++) {
      $id .= $tokens[array_rand($tokens)];
    }
    return $id;
  } // end randomString








  /**
   * quotes2entity replaces all " in the string by &quot;
   * It is to be used in all HTML attributes such as title, longdesc, 
   * value in a form, etc.
   *
   * @param $string the string to recode 
   * @return transformed string
   */
  public static function quotes2entity($string) {
    return str_replace("\"", "&quot;", $string);
  } // end quotes2entity





  /**
   * htmlEncodeString replaces all <, > and & by their HTML entities
   * @param $string string to encode
   * @param $charset character encoding, optionnal (defaults to utf-8)
   *
   * @return encoded tring
   */
  public static function htmlEncodeString($string, $charset = "utf-8") {
    return trim(htmlspecialchars($string, ENT_NOQUOTES, $charset));
  } // end htmlEncodeString





  /**
   * htmlEncodeArray recursively loops an array to encode <, > and & in
   * all entries (typically a POST data array)
   * 
   * @param $array passed by reference array 
   * @param $htmlOk list of array keys where HTML is accepted, hence 
   * where no encoding is needed
   *
   * @note no return value since array is passed by reference
   */

  public static function htmlEncodeArray(& $tab, $htmlOk = array()) {
    while (list($k, $v) = each($tab)) {
      if (!is_array($tab[$k])) {
	if (!in_array($k, $htmlOk)) {
	  $tab[$k] = Outils_Chaines::htmlEncodeString($tab[$k]);
	}
      } else {
	if (in_array($k, $htmlOk)) {
	  $htmlOk = array_merge($htmlOk, array_keys($tab[$k]));
	} 
	self::htmlEncodeArray($tab[$k], $htmlOk);
      }
    }
  } // end htmlEncodeArray
  
  public static function dateFr($date) {
	$tab = explode("-",$date);
	$annee = $tab[0];
	$mois = $tab[1];

	if($mois==1)$mois='janvier';
	if($mois==2)$mois='fevrier';
	if($mois==3)$mois='mars';
	if($mois==4)$mois='avril';
	if($mois==5)$mois='mai';
	if($mois==6)$mois='juin';
	if($mois==7)$mois='juillet';
	if($mois==8)$mois='aout';
	if($mois==9)$mois='septembre';
	if($mois==10)$mois='octobre';
	if($mois==11)$mois='novembre';
	if($mois==12)$mois='decembre';

	$tabjour = explode(" ",$tab[2]);
	$jour = $tabjour['0'];
	return $datefr=$jour." ".substr($mois,0,3)." ".$annee;

} 

	public static function urlRewriting($url){ /* A tester et à compléter selon les exceptions */
		$tabslash = array("cat","a");
		$urlmodif = "";
		if (preg_match('/(.*)index.php(.*)/',$url,$tabUrl)){
			$urlserveur = $tabUrl[1];
			$query = substr($tabUrl[2],1);
			$tabvar_val = explode('&',$query);
			$tabquery = array();
			if ($tabvar_val){
				foreach($tabvar_val as $q){
					$temptab = explode('=',$q);
					$tabquery[$temptab[0]] = $temptab[1];
				}
			} else {
				$temptab = explode('=',$q);
				$tabquery[$temptab[0]] = $temptab[1];
			}
			foreach($tabquery as $k=>$v){
				if (in_array($k,$tabslash)){
					$urlmodif .= $v . "/";
				} else {
					$urlmodif .= $v . "-";
				}
			}
			$urlmodif = preg_replace('/-($)/',".html",$urlmodif);
		} else {
			return $url;
		}
		return $urlserveur . $urlmodif;
		
	}


} // end class

?>